﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class ManageClient : ManageRibbonForm
    {
        int iSelectedIndex = -1;
        List<Client> _clients = new List<Client>();

        public ManageClient()
        {
            InitializeComponent();

            FillToGrid();

            this.WindowState = FormWindowState.Maximized;
        }

        private void FillToGrid()
        {
            _clients = Common.Entity.Clients.ToList();
            lstClient.Items.Clear();

            foreach (Client client in _clients)
            {
                client.ContactReference.Load();
                var listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
                                                                                           client.ClientCode,
                                                                                           client.ClientName,
                                                                                           client.Contact.ContactFirstName + " " + client.Contact.ContactMiddleName + " " + client.Contact.ContactLastName,
                                                                                           client.Contact.ContactCountry,
                                                                                           client.CreatedDate.ToShortDateString(),
                                                                                           "Open"}, -1);
                this.lstClient.Items.Add(listViewItem1);              
            }
        }

        private void ManageClient_Load(object sender, EventArgs e)
        {
            //lstClient.Items[1].BackColor = Color.FromArgb(80, 150, 150, 150);
        }

        private void lstClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstClient.SelectedIndices.Count > 0)
            {
                iSelectedIndex = lstClient.SelectedIndices[0];

                if (lstClient.Items[lstClient.SelectedIndices[0]].SubItems[5].Text == "Open")
                {
                    ((DStarter)this.MdiParent).ChangeActiveText("Deac&tive");
                }
                else
                {
                    ((DStarter)this.MdiParent).ChangeActiveText("Ac&tive");
                }
            }
            else
                iSelectedIndex = -1;
        }

        private void lstClient_DoubleClick(object sender, EventArgs e)
        {
            NewClientFrm frmNewClient;
            if (lstClient.SelectedItems.Count > 0)
            {
                frmNewClient = new NewClientFrm(_clients[lstClient.SelectedIndices[0]]);

                frmNewClient.ShowDialog();
            }
            Reload();
        }

        private void ManageClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            ((DStarter)this.MdiParent).SetRPEventVisible(false);
        }

        #region Overrides of ManageRibbonForm

        public override void Add()
        {
            NewClientFrm frmNewClient = new NewClientFrm();
            frmNewClient.ShowDialog();
            Reload();
        }

        public override void Edit()
        {
            //MessageBox.Show("Em code show form 'New Client' va dien thong tin tuong ung vao");
            NewClientFrm frmNewClient;
            if (lstClient.SelectedItems.Count > 0)
            {
                frmNewClient = new NewClientFrm(_clients[lstClient.SelectedIndices[0]]);

                frmNewClient.ShowDialog();
            }
            Reload();
        }

        public override void Delete()
        {
            if (lstClient.SelectedIndices.Count > 0)
            {
                Common.Entity.DeleteObject(_clients[lstClient.SelectedIndices[0]]);
                lstClient.Items.RemoveAt(lstClient.SelectedIndices[0]);
                Common.Entity.SaveChanges();
            }
        }

        public override void Reload()
        {
            //MessageBox.Show("Reload data from database and fill data to list!", "Description for function", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Common.Entity.Refresh(System.Data.Objects.RefreshMode.ClientWins,Common.Entity.Clients);
            FillToGrid();
        }

        public override void Active()
        {
            if (((DStarter)this.MdiParent).GetActiveText() == "Ac&tive")
            {
                lstClient.Items[iSelectedIndex].SubItems[5].Text = "Open";
                lstClient.Items[iSelectedIndex].BackColor = Color.White;
                _clients[iSelectedIndex].ClientStatus = 1;
            }
            else
            {
                lstClient.Items[iSelectedIndex].SubItems[5].Text = "Close";
                lstClient.Items[iSelectedIndex].BackColor = Color.FromArgb(80, 150, 150, 150);
                _clients[iSelectedIndex].ClientStatus = 0;
            }
            Common.Entity.SaveChanges();
        }

        public override void ViewContact()
        {
            NewContact newContact;
            if (lstClient.SelectedItems.Count != 0)
            {
                newContact = new NewContact(_clients[lstClient.SelectedIndices[0]]);

                newContact.ShowDialog();
            }
            Reload();
        }

        public override void ViewMatter()
        {
            PUMatterFrm puMatterFrm = new PUMatterFrm();
            puMatterFrm.ShowDialog();
        }
        #endregion

        private void ManageClient_Activated(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetRPGClientVisible(true);
            ((DStarter)this.MdiParent).SetEventFocus();
        }

        private void ManageClient_Deactivate(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetRPGClientVisible(false);
        }

        private void lstClient_Click(object sender, EventArgs e)
        {
            ((DStarter)this.MdiParent).SetEventFocus();
        }
    }
}
